addEventHandler("onClientVehicleStartEnter", getRootElement(), function(player, seat, door)
    if player ~= localPlayer then return end
    if seat ~= 0 then return end
    local vehicle = source
    local uid = tonumber(getElementData(localPlayer, "player:data").uid)
    local data = getElementData(vehicle, "vehicle:data")
    if not data or not uid then return end
    if tonumber(data.ownedPlayer) == uid then return end
    for i, v in ipairs(data.rent) do
        if tonumber(v) == uid then return end
    end
    cancelEvent()
end)

function createVehicleBlip(vehicle)
    createBlipAttachedTo(vehicle, 0, 2, 0, 0, 255, 225, 0)
end
addEvent("createVehicleBlip", true)
addEventHandler("createVehicleBlip", getRootElement(), createVehicleBlip)

function removeVehicleBlip(vehicle)
    for i, v in ipairs(getAttachedElements(vehicle)) do
        destroyElement(v)
    end
end
addEvent("removeVehicleBlip", true)
addEventHandler("removeVehicleBlip", getRootElement(), removeVehicleBlip)

addEvent("cmta_vehicles:clear", true)
addEventHandler("cmta_vehicles:clear", root, function(veh)
	local shader = dxCreateShader( ":cmta-shadery/shader.fx" )
	engineApplyShaderToWorldTexture( shader, "vehiclegrunge256", veh )
	engineApplyShaderToWorldTexture( shader, "?emap*", veh )
	
    local data = getElementData(veh, "vehicle:data")
		local shaderid = tonumber(data.shader)
		if tonumber(shaderid) ~= 0 and data.shader then
				vehTexture = dxCreateTexture( ":cmta-paintjoby/files/"..shaderid..".png" )
				shader_carsstart, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
				engineRemoveShaderFromWorldTexture ( shader_carsstart, "vehiclegrunge256", veh )
				engineApplyShaderToWorldTexture ( shader_carsstart, "vehiclegrunge256", veh )
				 dxSetShaderValue ( shader_carsstart, "TX0", vehTexture ) 	
		end
end)

function clear()
   for i,v in ipairs(getElementsByType("vehicle")) do
	if getElementData(v, "vehicle:data") then
	local data = getElementData(v, "vehicle:data")
    if data.id then  
	local shader = dxCreateShader( ":cmta-shadery/shader.fx" )
	engineApplyShaderToWorldTexture( shader, "vehiclegrunge256", v )
	engineApplyShaderToWorldTexture( shader, "?emap*", v )
	end
end
   end
   for i,v in ipairs(getElementsByType("vehicle")) do
	if getElementData(v, "vehicle:data") then
    local data = getElementData(v, "vehicle:data")
	local shaderid = tonumber(data.shader)
	if tonumber(shaderid) ~= 0 and data.id then
			vehTexture = dxCreateTexture( ":cmta-paintjoby/files/"..shaderid..".png" )
			shader_carsstart, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
			engineRemoveShaderFromWorldTexture ( shader_carsstart, "vehiclegrunge256", v )
			engineApplyShaderToWorldTexture ( shader_carsstart, "vehiclegrunge256", v )
			 dxSetShaderValue ( shader_carsstart, "TX0", vehTexture ) 	
			end
	end
end
end
addEventHandler("onClientResourceStart",resourceRoot,clear)

